package tutorial.ibatis.example1;

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import tutorial.ibatis.example1.model.Automobile;

public class Main {

	private static final String CREATE = "create";
	private static final String DELETE = "delete";
	private static final String IBATIS_CONFIG = "tutorial/ibatis/example1/ibatis-config.xml";
//	private static final String IBATIS_CONFIG = "resources/ibatis-config.xml";
	private static final String SHOW = "show";
	private static final String SHOWALL = "showall";
	private static final String SHOWORG = "showorg";

	@SuppressWarnings("static- access")
	private static Options createOptions() {
		Options options = new Options();
		options.addOption(CREATE, false, "creates the objects in the database");
		options.addOption(OptionBuilder.withArgName(SHOW).hasArg()
				.withDescription("shows the specified  automobile")
				.withLongOpt(SHOW).create());
		options.addOption(DELETE, false,
				"deletes all of the objects in  database");
		options.addOption(SHOWALL, false,
		"Show all of the objects in  database");
		options.addOption(SHOWORG, false,
		"Show all of the org in  database");
		return options;
	}

	private static SqlSessionFactory createSqlMapper() throws IOException {
		Reader reader = Resources.getResourceAsReader(IBATIS_CONFIG);
		return new SqlSessionFactoryBuilder().build(reader);
	}

	public static void main(final String[] args) {
		Options options = createOptions();
		try {
			CommandLine cmd = new GnuParser().parse(options, args);

			SqlSession session = createSqlMapper().openSession();

			try {
				if (cmd.hasOption(CREATE)) {
					System.out
							.println("Creating  the objects in the database...");
					// Create the automobiles
					session.insert(Automobile.class.getName() + ".insert",
							new Automobile(1, "Toyota", "Tercel", 1993));
					session.insert(Automobile.class.getName() + ".insert",
							new Automobile(2, "Honda", "CR-V", 2000));
					session.insert(Automobile.class.getName() + ".insert",
							new Automobile(3, "Chevrolet", "Impala", 1964));
					session.insert(Automobile.class.getName() + ".insert",
							new Automobile(4, "Dodge", "Pickup", 1946));

					session.commit();

				} else if (cmd.hasOption(SHOW)) {
					Automobile auto = (Automobile) session.selectOne(
							Automobile.class.getName() + ".select",
							cmd.getOptionValue(SHOW));
					if (auto == null) {
						System.out.println("No matching results found!");
					} else {
						System.out.println(auto);
					}

				} else if (cmd.hasOption(SHOWALL)) {
					List<Automobile> autos = session
							.selectList(Automobile.class.getName()
									+ ".selectAll");
					Iterator<Automobile> ite = autos.iterator();
					while(ite.hasNext()){
						System.out.println(ite.next().toString());
					}

				} else if (cmd.hasOption(SHOWORG)){
					List autos = session.selectList(Automobile.class.getName()
							+ ".selectOrg");
					Iterator ite = autos.iterator();
					while(ite.hasNext()){
						System.out.println(ite.next());
					}
					
				}else if (cmd.hasOption(DELETE)) {

					session.delete(Automobile.class.getName() + ".deleteAll");
					session.commit();

				} else {
					System.out.println("Doing nothing.");
				}

			} finally {
				session.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}